home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / setextMode.tcl < prev    next >
Encoding:
Text File  |  2000-12-28  |  19.5 KB  |  570 lines

  1. ## -*-Tcl-*-  (nowrap)
  2.  # ==========================================================================
  3.  #  FILE: "setextMode.tcl"
  4.  #  
  5.  #  Setext file support
  6.  #
  7.  #  Recognize and automatically mark 'setext'-encoded text files, like Tidbits.
  8.  #  
  9.  #                                    created: 10/01/94 {09:51:15 pm} 
  10.  #                                last update: 12/28/2000 {09:53:09 AM} 
  11.  #  
  12.  #  Author: Craig Barton Upright
  13.  #  E-mail: <cupright@princeton.edu>
  14.  #    mail: Princeton University
  15.  #          2.N.1 Green Hall,  Princeton, New Jersey  08544
  16.  #     www: <http://www.princeton.edu/~cupright>
  17.  #  
  18.  # -------------------------------------------------------------------
  19.  #  
  20.  # Copyright (c) 2000  Tom Pollard, Craig Barton Upright
  21.  # 
  22.  # This program is free software; you can redistribute it and/or modify
  23.  # it under the terms of the GNU General Public License as published by
  24.  # the Free Software Foundation; either version 2 of the License, or
  25.  # (at your option) any later version.
  26.  # 
  27.  # This program is distributed in the hope that it will be useful,
  28.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  # GNU General Public License for more details.
  31.  # 
  32.  # You should have received a copy of the GNU General Public License
  33.  # along with this program; if not, write to the Free Software
  34.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  # 
  36.  # ==========================================================================
  37.  ##
  38.  
  39. # ===========================================================================
  40. #
  41. # Explanation of what Setext is and does
  42. # ======================================
  43. # Setext stands for Structure Enhanced Text.  It is a markup scheme for
  44. # plain text documents such as email messages and e-zines.  Setext's
  45. # primary goal is to provide a way of marking text that is visually
  46. # unobtrusive, so that if you don't have a special setext browser, like
  47. # EasyView, you can still read the text.  (Have you ever tried to make
  48. # sense of HTML source without your web browswer?)
  49. # The document Setext Help is a description of setext concepts written by
  50. # Ian Feldman.  Setext grabbed a foothold in the Mac world with the online
  51. # publication TidBITS. Rudimentary setext browsers were built with
  52. # HyperCard for reading TidBITS. Setext seems to be merely a historical
  53. # curiousity now.
  54. # (NOTE: Setext is easier to use with mono-spaced fonts like Monoco.)
  55. #    -Donavan Hall, Tuesday, March 14, 2000
  56. # ------------------------------------------------------------------
  57. # Version 1.2   of Setx mode
  58. # ==========================
  59. # After Donavan sent me a Setext file for the Mode Examples folder, with an
  60. # excellent description of what Setext is (was?), I have developed a
  61. # fondness for the mode and use it exclusively for various README.TXT files
  62. # that I often write to annotate my work -- as Donavan mentions, the
  63. # marking function is quite handy.  I decided to update the mode, allowing
  64. # the user to set additional preferences, including some comment character,
  65. # magic character, keyword definition and colorizing.
  66. # In some ways, Setx could be thought of as "Text2", with the same
  67. # functionality, but greater customization available.  If Alpha does not
  68. # have a mode that users need, Setx could be adapted to serve as a
  69. # surrogate until they've convinced someone to write one for them.  The
  70. # current version of Setx, orginally written by Tom Pollard, is the
  71. # long-awaited 1.2 .
  72. # The default mode preferences are intended to show off some of Setx's new
  73. # functionality, which can be observed in the Setext Help file.
  74. # For more information see the Setext Help file.
  75. #    - Craig Barton Upright, 12 April, 2000
  76.  
  77. # ===========================================================================
  78. #
  79. # ◊◊◊◊ Initialization of Setx mode ◊◊◊◊ #
  80.  
  81. alpha::mode Setx 2.0 SetxMenu {*.stx *.etx} {
  82.     electricReturn
  83. } {
  84.     # We require 7.4b21 for prefs handling.
  85.     alpha::package require AlphaTcl 7.4b21
  86. } uninstall {
  87.     this-file
  88. } help {
  89.     file "Setext Help"
  90. } maintainer {
  91.     "Craig Barton Upright" <cupright@princeton.edu> 
  92.     <http://www.princeton.edu/~cupright/>
  93. }
  94.  
  95. proc setextMode.tcl {} {}
  96.  
  97. proc SetxMenu {} {}
  98.  
  99. namespace eval Setx {}
  100.  
  101. # ===========================================================================
  102. #
  103. # ◊◊◊◊ Setting Setx mode variables ◊◊◊◊ #
  104. #
  105. # I tried to write this mode allowing the user to make any necessary
  106. # modifications through the Mode Prefs dialog.  The commented explanations
  107. # given above each of the following preferences will appear when the user
  108. # clicks the "Help" button of the dialog.
  109. # Setting comment, magic character, and keyword preferences to show off
  110. # Setx's capabilities in the Setxt Help file.
  111.  
  112. # Removing obsolete preferences from earlier versions.
  113.  
  114. set oldvars {
  115.     don'tRemindMe
  116. }
  117.  
  118. foreach oldvar $oldvars {prefs::removeObsolete SetxmodeVars($oldvar)}
  119.  
  120. unset oldvars
  121.  
  122. #=============================================================================
  123. #
  124. # Standard preferences recognized by various Alpha procs
  125. #
  126.  
  127. newPref flag autoMark              {0}     Setx
  128. newPref var  fillColumn            {75}    Setx
  129. newPref var  wordBreak             {\w+}   Setx
  130. newPref var  wordBreakPreface      {(\W)}  Setx
  131. newPref flag wordWrap              {1}     Setx
  132.  
  133. #=============================================================================
  134. #
  135. # Flag preferences
  136. #
  137.  
  138. # Check this box to use the defined paired comment characters in Comment
  139. # Line / Box / Paragraph menu items.
  140. newPref flag usePairedComments     {0}     Setx    {Setx::updatePreferences}
  141.  
  142. #=============================================================================
  143. #
  144. # Variable preferences
  145.  
  146. # Everything from the Comment Character(s) to the end of the current line
  147. # will be colorized according to the "Comment Color".  This should agree
  148. # with the Prefix String below.
  149. newPref var commentCharacter      {#}     Setx    {Setx::updatePreferences}
  150.  
  151. # Select the opening character(s) of a bracketed comment. 
  152. newPref var commentPair1          {/*}    Setx    {Setx::updatePreferences}
  153.  
  154. # Select the ending character(s) of a bracketed comment.  These cannot be
  155. # the same as the opening characters.
  156. newPref var commentPair2          {*/}    Setx    {Setx::updatePreferences}
  157.  
  158. # Define the indentation string for Section marks.
  159. newPref var indentString          {   }   Setx
  160.  
  161. # Setx allows for three levels of keywords.  Shorter lists can be entered
  162. # here in the preferences.  For longer lists, see the Setext Help file for
  163. # instructions on editing a SetxPrefs.tcl file.
  164. newPref var keywords1     {setx}          Setx    {Setx::updatePreferences}
  165. newPref var keywords2     {}              Setx    {Setx::updatePreferences}
  166. newPref var keywords3     {See the Setext Help file for more information.}        Setx    {Setx::updatePreferences}
  167.  
  168. # Magic Characters will colorize any string which follows them, using the
  169. # "symbol" color.  Only one Magic Character can be defined.
  170. newPref var magicCharacter        {$}     Setx    {Setx::updatePreferences}
  171.  
  172. # Select a Prefix String for commenting lines.  This should agree with the
  173. # Comment Character above, but also have a space after the character.
  174. newPref var prefixString          {# }    Setx
  175.  
  176. # Command double-clicking will send the highlighted text to this search
  177. # engine.
  178. newPref url searchUrl1 {http://www.google.com/search?q=} Setx
  179.  
  180. # Command double-clicking while pressing the "option" key will send the
  181. # highlighted text to this search engine.
  182. newPref url searchUrl2 {http://www.go.com/Split?sv=IS&lk=noframes&qt=} Setx
  183.  
  184. # Command double-clicking while pressing the "control" key will send the
  185. # highlighted text to this search engine.
  186. newPref url searchUrl3 {http://search.metacrawler.com/crawler?general=} Setx
  187.  
  188. # Command double-clicking while pressing the "shift" key will send the
  189. # highlighted text to this search engine.
  190. newPref url searchUrl4 {http://northernlight.com/nlquery.fcg?si=&cb=0&qr=} Setx
  191.  
  192. # Additional characters to be colorized by the "Symbol Color".  The "-" and
  193. # "=" symbols will always be included.
  194. newPref var symbols               {@ %}   Setx    {Setx::updatePreferences}
  195.  
  196. # ===========================================================================
  197. #
  198. # Color preferences
  199. #
  200. # Since I want to put a message in the keyword3 box, I am setting that
  201. # color black (which will show up as "none").
  202. #
  203.  
  204. newPref color commentColor      {red}       Setx    {Setx::updatePreferences}
  205. newPref color keyword1Color     {magenta}   Setx    {Setx::updatePreferences}
  206. newPref color keyword2Color     {black}     Setx    {Setx::updatePreferences}
  207. newPref color keyword3Color     {black}     Setx    {Setx::updatePreferences}
  208.  
  209. # Color of the user defined magic character.
  210. newPref color magicColor        {blue}      Setx    {Setx::updatePreferences}
  211.  
  212. # Strings are any words that appear between double quotes on the same line.
  213. newPref color stringColor       {green}     Setx    {Setx::updatePreferences}
  214.  
  215. # This preference colorizes the = and - strings which indicate that the
  216. # line above is a heading or subheading, and any other symbols defined by
  217. # the user in "Symbols".
  218. newPref color symbolColor       {blue}      Setx    {Setx::updatePreferences}
  219.  
  220. # ===========================================================================
  221. #
  222. # Update Preferences.  
  223. # This allows for changes to take effect without a restart.
  224. # Danger:  Don't include this proc in any {mode}Prefs.tcl file !!!
  225. # This will source the prefs file, and thus put Alpha in an endless loop.
  226. # Instead, use a <mode>::colorize<mode> proc in the prefs file.
  227. #
  228.  
  229. proc Setx::updatePreferences    {flag} {
  230.     
  231.     global mode PREFS $flag SetxmodeVars 
  232.     
  233.     # If the mode has a {mode}Prefs.tcl file, we want to load that as 
  234.     # well, otherwise any keywords contained therein won't be updated
  235.     # without a manual "Load Prefs File".
  236.     
  237.     if {[file exists [file join ${PREFS} ${mode}Prefs.tcl]]} {
  238.         uplevel #0 [list source [file join ${PREFS} ${mode}Prefs.tcl]]
  239.     } 
  240.     
  241.     Setx::colorizeSetx
  242.     Setx::commentMenuItems
  243.     refresh
  244.     
  245.     # Alertnote notes after certain preferences have been changed.  Once a
  246.     # keyword has been identified, it cannot be "unloaded" until a restart.
  247.     
  248.     if {($flag == "keywords1")      || 
  249.         ($flag == "keywords2")      ||  
  250.         ($flag == "keywords3")      ||  
  251.         ($flag == "symbols")           } {
  252.         
  253.         alertnote "Deletions from $flag preference\
  254.           will only take effect after a restart.\
  255.           Keywords cannot be 'unloaded.'"
  256.     }
  257. }
  258.  
  259. # ===========================================================================
  260. # Comment Character variables for Comment Line / Paragraph / Box menu items.
  261.  
  262. proc Setx::commentMenuItems {} {
  263.     
  264.     global SetxmodeVars Setx::commentCharacters
  265.     
  266.     if {$SetxmodeVars(usePairedComments)} {
  267.         
  268.         # determining what the "middle comment character" is in a pair.
  269.         
  270.         if {[string length $SetxmodeVars(commentPair1)] == 2} {
  271.             set mCC [string index $SetxmodeVars(commentPair1) 1]
  272.         } else {
  273.             set mCC {*}
  274.         }
  275.         
  276.         set Setx::commentCharacters(General)           \
  277.           "$mCC"
  278.         
  279.         set Setx::commentCharacters(Paragraph)          \
  280.           [list  "$SetxmodeVars(commentPair1) "         \
  281.           " $SetxmodeVars(commentPair2)"                \
  282.           " $mCC "]
  283.         
  284.         set Setx::commentCharacters(Box)                \
  285.           [list  "$SetxmodeVars(commentPair1)" 1        \
  286.           "$SetxmodeVars(commentPair2)" 1               \
  287.           "$mCC" 3]
  288.         
  289.    } else {
  290.        set cC $SetxmodeVars(commentCharacter)
  291.         set Setx::commentCharacters(General)            \
  292.           "$cC "
  293.           
  294.         set Setx::commentCharacters(Paragraph)          \
  295.           [list  "$cC$cC " \
  296.           " $cC$cC" \
  297.           " $cC "]
  298.           
  299.         set Setx::commentCharacters(Box)                \
  300.           [list  "$cC" 1    \
  301.           "$cC" 1           \
  302.           "$cC" 3]
  303.     }
  304. }
  305.  
  306. # Call this now.
  307.  
  308. Setx::commentMenuItems
  309.  
  310. # ===========================================================================
  311. # ◊◊◊◊ Colorize Setx ◊◊◊◊ #
  312. # Used in updatePreferences, and could be called in a <mode>Prefs.tcl file
  313.  
  314. proc Setx::colorizeSetx {} {
  315.     
  316.     global SetxmodeVars Setxcmds
  317.     
  318.     set Setxcmds [lsort [lunique [concat \
  319.       $SetxmodeVars(keywords1) \
  320.       $SetxmodeVars(keywords2) \
  321.       $SetxmodeVars(keywords3) \
  322.       ]]]
  323.         
  324.     # Keywords 1
  325.     
  326.     regModeKeywords -a                          \
  327.       -e $SetxmodeVars(commentCharacter)        \
  328.       -b $SetxmodeVars(commentPair1)            \
  329.          $SetxmodeVars(commentPair2)            \
  330.       -c $SetxmodeVars(commentColor)            \
  331.       -s $SetxmodeVars(stringColor)             \
  332.       -k $SetxmodeVars(keyword1Color)  Setx     \
  333.       $SetxmodeVars(keywords1)
  334.         
  335.     # Keywords 2
  336.     
  337.     regModeKeywords -a                          \
  338.       -k $SetxmodeVars(keyword2Color)  Setx     \
  339.       $SetxmodeVars(keywords2) 
  340.     
  341.     # Keywords 3
  342.         
  343.     regModeKeywords -a                          \
  344.       -k $SetxmodeVars(keyword3Color)  Setx     \
  345.       $SetxmodeVars(keywords3)
  346.         
  347.     # Symbols, Magic Character
  348.     
  349.     regModeKeywords -a                          \
  350.       -m $SetxmodeVars(magicCharacter)          \
  351.       -k $SetxmodeVars(magicColor)  Setx        \
  352.       $SetxmodeVars(symbols)                    \
  353.       -i "=" -i "-"                             \
  354.       -I $SetxmodeVars(symbolColor)
  355. }
  356.  
  357. # This is a "dummy" command, necessary for the above proc so that all of
  358. # the "regModeKeywords" commands in the called color procs can be "adds"
  359. # (-a).  When the mode is first invoked, this has to occur before the color
  360. # procs are called.
  361.  
  362. regModeKeywords -k {none} Setx {}
  363.  
  364. # now we finally colorize
  365.  
  366. Setx::colorizeSetx
  367.  
  368. # ===========================================================================
  369. # ◊◊◊◊ Key Bindings, Electrics ◊◊◊◊ #
  370.  
  371. Bind '-' <c> {Setx::underline -} Setx
  372. Bind '=' <c> {Setx::underline =} Setx
  373.  
  374. proc Setx::underline {symbol} {
  375.  
  376.     goto [set pos [lineStart [getPos]]]
  377.     endLineSelect
  378.  
  379.     # First convert all tabs to spaces.
  380.     tabsToSpaces
  381.     goto $pos
  382.     endLineSelect
  383.  
  384.     # Now remove any stray spaces from the end of the current line.
  385.     replaceText $pos [selEnd] [string trimright [getSelect]]
  386.     goto $pos
  387.     endLineSelect
  388.     
  389.     # Now substitute the symbol for any character, and insert a new
  390.     # line below the current one.
  391.     regsub -all {.} [getSelect] $symbol symbolLine
  392.     goto [selEnd]
  393.     insertText "\r${symbolLine}\r"
  394.     markFile
  395. }
  396.  
  397. # Setting the order of precedence for completions.
  398.  
  399. set completions(Setx) {
  400.     completion::cmd completion::electric completion::word
  401. }
  402.  
  403. # Using any defined keywords for completions
  404.  
  405. set Setxcmds [lsort [concat                                             \
  406.   $SetxmodeVars(keywords1) $SetxmodeVars(keywords2) $SetxmodeVars(keywords3)]]
  407.  
  408.  
  409. # ===========================================================================
  410. # ◊◊◊◊ Command Double Click ◊◊◊◊ #
  411. # Send the highlighted text to the defined search engine.
  412.  
  413. proc Setx::DblClick {from to shift option control} {
  414.     
  415.     global SetxmodeVars
  416.     
  417.     select $from $to
  418.     set command [getSelect]
  419.     regsub -all { } $command {+} commandPlus
  420.     set commandPlus [concat %22$commandPlus%22]
  421.  
  422.     # Any modifiers pressed?
  423.     if {$option && $SetxmodeVars(searchUrl2) != ""} {
  424.         message "\"$command\" sent to $SetxmodeVars(searchUrl2)"
  425.         url::execute $SetxmodeVars(searchUrl2)$commandPlus
  426.     } elseif {$control && $SetxmodeVars(searchUrl3) != ""} {
  427.         message "\"$command\" sent to $SetxmodeVars(searchUrl3)"
  428.         url::execute $SetxmodeVars(searchUrl3)$commandPlus
  429.     } elseif {$shift && $SetxmodeVars(searchUrl4) != ""} {
  430.         message "\"$command\" sent to $SetxmodeVars(searchUrl4)"
  431.         url::execute $SetxmodeVars(searchUrl4)$commandPlus
  432.     } elseif {$SetxmodeVars(searchUrl1) != ""} {
  433.         message "\"$command\" sent to $SetxmodeVars(searchUrl1)"
  434.         url::execute $SetxmodeVars(searchUrl1)$commandPlus
  435.     } else {
  436.         message "The search url preference for this modifier has not been set."
  437.     } 
  438. }
  439.  
  440. # ===========================================================================
  441. # ◊◊◊◊ Mark File Proc ◊◊◊◊ #
  442. # author:  Tom Pollard
  443. # Changes made by cbu:
  444. #  -- changes the sub-heading to only indent three spaces, not four
  445. #  -- strip comment character, spaces from the beginning of any mark name
  446. #  
  447. # How the Mark File works
  448. # Any two lines that look like this:
  449. # Any string of words
  450. # ===================
  451. # will be marked as a Chapter heading.  Any two lines that look like this:
  452. # Any other string of words
  453. # -------------------------
  454. # will be marked as a Section heading.  That's all there is to it.  
  455.  
  456. proc Setx::MarkFile {} {
  457.     
  458.     removeAllMarks
  459.     message "Marking File …"
  460.     global SetxmodeVars 
  461.     
  462.     set cC $SetxmodeVars(commentCharacter)
  463.     set iS $SetxmodeVars(indentString)
  464.     
  465.     set pat1 {^(-+|=+)$}
  466.     set end [maxPos]
  467.     set pos [minPos]
  468.     set count1 0
  469.     set count2 0
  470.     set l {}
  471.     while {![catch {search -s -f 1 -r 1 -m 0 -i 1 $pat1 $pos} match]} {
  472.         set pos1 [lineStart [pos::math [lindex $match 0] - 1]]
  473.         set marker [string trimright [eval getText $match]]
  474.         set line [string trimright [getText $pos1 [nextLineStart $pos1]]]
  475.         if {[string length $line] == [string length $marker]} {
  476.             # Strip any leading comment characters, spaces from the mark
  477.             set line [string trimleft $line "$cC "]
  478.             if {[string range $marker 0 0] == "-"} {
  479.                 set line "$iS$line"
  480.                 incr count2
  481.             } else {
  482.                 incr count1
  483.             }
  484.             regsub {/} $line {-} line
  485.             set inds($line) $pos1
  486.             lappend sects $line
  487.         }
  488.         set pos [nextLineStart [lindex $match 1]]
  489.     }
  490.     
  491.     if {[info exists inds]} {
  492.         foreach f $sects {
  493.             set next [lineStart $inds($f)]
  494.             setNamedMark $f $inds($f) $next $next
  495.         }
  496.     }
  497.     message "This file contains $count1 chapters, $count2 sections."
  498. }
  499.  
  500. # ===========================================================================
  501. #
  502. # ◊◊◊◊ Version History ◊◊◊◊ #
  503. #  modified by  rev    reason
  504. #  -------- --- ------ -----------
  505. #  10/01/94 tp  1.0.1  First version of Setx mode written by Tom Pollard
  506. #  04/02/00 cbu 1.0.2  Additional preferences added, allowing user to define a
  507. #                        comment character, magic character, keyword dictionaries
  508. #  04/06/00 cbu 1.1    Added "Update Colors" proc to avoid need for a restart
  509. #  04/20/00 cbu 1.1.1  Added "Use Paired Comments" variable for menu items.
  510. #                      Added "Comment Menu Items" proc to update commentCharacter sets.
  511. #  06/22/00 cbu 1.2    Reorganized Color proc routines.
  512. #                      Renamed "Update Colors" to "Update Preferences".
  513. #                      Fixed the "middle comment character" dilemna in paired 
  514. #                        comments.
  515. #                      Moved "refresh" from Colorize to Update Preferences to 
  516. #                        avoid "no open window" bug from ever coming up.
  517. #                      Mark names are stripped of leading comment characters, 
  518. #                        spaces. This way one can colorize headings using 
  519. #                        comment character.
  520. #                      Section marks indentation now a variable.
  521. #  12/04/00 cbu 1.3    Added Setx::DblClick for search urls.
  522. #  12/04/00 cbu 2.0    New url prefs handling requires 7.4b21
  523. #                      Added Bernard's bindings, Setx::underline
  524.  
  525. # ===========================================================================
  526. #
  527. # .